package no.uib.olsdialog.util;

import no.uib.olsdialog.OLSDialog;
import javax.swing.*;

/**
 * A simple dialog displaying a list of the most common NEWT species.
 *
 * @author Harald Barsnes
 */
public class SimpleNewtSelection extends javax.swing.JDialog {

    /**
     * The OLS dialog.
     */
    private OLSDialog olsDialog;

    /**
     * Creates a new SimpleNewtSelection dialog and makes it visible.
     *
     * @param olsDialog a reference to the OLS Dialog
     * @param modal
     */
    public SimpleNewtSelection(OLSDialog olsDialog, boolean modal) {
        super(olsDialog, modal);
        initComponents();
        this.olsDialog = olsDialog;
        mostCommonSpeciesJComboBox.setRenderer(new MyComboBoxRenderer(null, SwingConstants.CENTER));
        setLocationRelativeTo(olsDialog);
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        mostCommonSpeciesJComboBox = new javax.swing.JComboBox();
        okJButton = new javax.swing.JButton();
        cancelJButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("NEWT Species Selection");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Most Common Species", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        mostCommonSpeciesJComboBox.setMaximumRowCount(20);
        mostCommonSpeciesJComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Aedes aegypti (Yellowfever mosquito) [7159]", "Anopheles gambiae (African malaria mosquito) [7165]", "Apis mellifera (Honeybee) [7460]", "Arabidopsis thaliana (Mouse-ear cress) [3702]", "Bos taurus (Cow) [9913]", "Caenorhabditis briggsae [6238]", "Caenorhabditis elegans (Nematode) [6239]", "Canis familiaris (Dog) [9615]", "Cavia porcellus (Guinea pig) [10141]", "Ciona intestinalis (Transparent sea squirt) [7719]", "Ciona savignyi (Pacific transparent sea squirt) [51511]", "Danio rerio (Zebrafish) [7955]", "Dasypus novemcinctus (Nine-banded armadillo) [9361]", "Dipodomys ordii (Ord's kangaroo rat) [10020]", "Drosophila melanogaster (Fruit Fly) [7227]", "Echinops telfairi (Lesser hedgehog) [9371]", "Erinaceus europaeus (Western European hedgehog) [9365]", "Escherichia coli [562]", "Felis silvestris catus (Cat) [9685]", "Fugu rubripes (Japanese pufferfish) [31033]", "Gallus gallus (Chicken) [9031]", "Gasterosteus aculeatus (Stickleback) [69293]", "Hepatitis C virus [11103]", "Homo sapiens (Human) [9606]", "Human immunodeficiency virus 1 [11676]", "Lama guanicoe pacos (Alpaca) (Lama pacos) [30538]", "Loxodonta africana (African elephant) [9785]", "Macaca mulatta (Rhesus macaque) [9544]", "Microcebus murinus (Lesser mouse lemur) [30608]", "Monodelphis domestica (Opossum) [13616]", "Mus musculus (Mouse) [10090]", "Myotis lucifugus (Little brown bat) [59463]", "Ochotona princeps (Southern American pika) [9978]", "Ornithorhynchus anatinus (Duckbill platypus) [9258]", "Oryctolagus cuniculus (Rabbit) [9986]", "Oryza sativa (Rice) [39947]", "Oryzias latipes (Medaka fish) [8090]", "Otolemur garnettii (Bushbaby) [30611]", "Pan troglodytes (Chimp) [9598]", "Paramecium tetraurelia [5888]", "Procavia capensis (Cape hyrax) (Rock dassie) [9813]", "Pteropus vampyrus (Large flying fox) [132908]", "Rattus norvegicus (Rat) [10116]", "Saccharomyces cerevisiae (Baker's yeast) [4932]", "Sorex araneus (Common Shrew) [42254]", "Spermophilus tridecemlineatus (Squirrel) [43179]", "Strongylocentrotus purpuratus (Purple sea urchin) [7668]", "Tarsius syrichta (Philippine tarsier) [9478]", "Tetraodon nigroviridis (Green puffer) [99883]", "Trypanosoma cruzi [5693]", "Tupaia glis belangeri (Common tree shrew) [37347]", "Tursiops truncatus (Atlantic bottle-nosed dolphin) [9739]", "Xenopus tropicalis (Western clawed frog) [8364]"}));
        mostCommonSpeciesJComboBox.setSelectedIndex(23);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .add(mostCommonSpeciesJComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 379, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .add(mostCommonSpeciesJComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(16, Short.MAX_VALUE))
        );

        okJButton.setText("OK");
        okJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okJButtonActionPerformed(evt);
            }
        });

        cancelJButton.setText("Cancel");
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeDialog(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                                .addContainerGap()
                                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .addContainerGap(289, Short.MAX_VALUE)
                                .add(okJButton)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(cancelJButton)
                                .addContainerGap())
        );

        layout.linkSize(new java.awt.Component[]{cancelJButton, okJButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                                .addContainerGap()
                                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                        .add(cancelJButton)
                                        .add(okJButton))
                                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the dialog.
     *
     * @param evt
     */
    private void closeDialog(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeDialog
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_closeDialog

    /**
     * Inserts the selected term into the OLS Dialog.
     *
     * @param evt
     */
    private void okJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okJButtonActionPerformed
        String selectedSpecies = mostCommonSpeciesJComboBox.getSelectedItem().toString();
        String termName = selectedSpecies.substring(0, selectedSpecies.indexOf("[") - 1);
        String termId = selectedSpecies.substring(selectedSpecies.indexOf("[") + 1, selectedSpecies.length() - 1);
        olsDialog.insertNewtSelection(termName, termId);
        closeDialog(null);
    }//GEN-LAST:event_okJButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelJButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox mostCommonSpeciesJComboBox;
    private javax.swing.JButton okJButton;
    // End of variables declaration//GEN-END:variables
}
